package com.example.myapplication.Fragment;

import android.content.Context;
import android.graphics.Outline;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.activity.LoadingActivity;
import com.example.myapplication.R;
import com.example.myapplication.adapter.recyclerViewAdapter.MusicHallHorAdapter;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.RecommendResource;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MusicHallFragment  extends Fragment {
    private View view;
    private RecyclerView recyclerView_hall;
    private List<RecommendResource.RecommendBean> recommendBeanList = new ArrayList<>();
    private MusicHallHorAdapter adapter;
    private Banner banner;
    private ArrayList<String> list_path;
    private Context context;
    private List<com.example.myapplication.entity.Banner.BannersBean> bannerList = new ArrayList<>();
    private ArrayList<Fragment> mFragmentList = new ArrayList<Fragment>();
    private Fragment mCurrentFragmen = null;  // 记录当前显示的Fragment

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_music_hall, container, false);
        init();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        TextView new_song = view.findViewById(R.id.new_song);
        TextView new_al = view.findViewById(R.id.new_al);
        TextView album = view.findViewById(R.id.album);
        View view2  = view.findViewById(R.id.view2);
        View view3  = view.findViewById(R.id.view3);
        View view11  = view.findViewById(R.id.view11);
        mFragmentList.add(0,new RecommentSongFragment());
        mFragmentList.add(1,new RecommentSongFragment());
        mFragmentList.add(2,new RecommentSongFragment());
        mCurrentFragmen = mFragmentList.get(0);

        // 初始化首次进入时的Fragment
        AppCompatActivity activity = (AppCompatActivity) view.getContext();
        activity.getSupportFragmentManager().beginTransaction()
                .add(R.id.container1, mFragmentList.get(0), null)
                .add(R.id.container1, mFragmentList.get(1), null)
                .add(R.id.container1, mFragmentList.get(2), null)
                .hide(mFragmentList.get(1))
                .hide(mFragmentList.get(2))
                .show(mFragmentList.get(0))
                .commit();

        new_song.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new_song.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                view2.setVisibility(View.VISIBLE);
                view3.setVisibility(View.INVISIBLE);
                view11.setVisibility(View.INVISIBLE);
                new_al.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                album.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                activity.getSupportFragmentManager().beginTransaction()
                        .hide(mCurrentFragmen)
                        .show(mFragmentList.get(0))
                        .commit();
                mCurrentFragmen = mFragmentList.get(0);
            }
        });

        new_al.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new_al.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                view2.setVisibility(View.INVISIBLE);
                view3.setVisibility(View.INVISIBLE);
                view11.setVisibility(View.VISIBLE);
                new_song.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                album.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                activity.getSupportFragmentManager().beginTransaction()
                        .hide(mCurrentFragmen)
                        .show(mFragmentList.get(1))
                        .commit();

                mCurrentFragmen = mFragmentList.get(1);
            }
        });

        album.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                album.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                view2.setVisibility(View.INVISIBLE);
                view3.setVisibility(View.VISIBLE);
                view11.setVisibility(View.INVISIBLE);
                new_song.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                new_al.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                activity.getSupportFragmentManager().beginTransaction()
                        .hide(mCurrentFragmen)
                        .show(mFragmentList.get(2))
                        .commit();
                mCurrentFragmen = mFragmentList.get(2);
            }
        });
    }

    private void initBanner() {
        banner = (Banner) view.findViewById(R.id.banner);
        //放图片地址的集合
        list_path = new ArrayList<>();
        banner.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 20);
            }
        });
        banner.setClipToOutline(true);
        for(int i=0;i<bannerList.size();i++){
            list_path.add(bannerList.get(i).getPic());
        }
        //设置内置样式，共有六种可以点入方法内逐一体验使用。
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器，图片加载器在下方
        banner.setImageLoader(new MyLoader());
        //设置图片网址或地址的集合
        banner.setImages(list_path);
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Default);
        //设置轮播图的标题集合
        //banner.setBannerTitles(list_title);

        //设置轮播间隔时间
        banner.setDelayTime(3000);
        //设置是否为自动轮播，默认是“是”。
        banner.isAutoPlay(true);
        //设置指示器的位置，小点点，左中右。
        banner.setIndicatorGravity(BannerConfig.CENTER)
                //必须最后调用的方法，启动轮播图。
                .start();


    }
    //自定义的图片加载器
    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context)
                    .load((String) path)
                    .apply(new RequestOptions().transform(new RoundedCorners(20)))//圆角半径
                    .into(imageView);
        }
    }

    private void init() {
        recyclerView_hall = view.findViewById(R.id.recyclerView_hall);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false);
        recyclerView_hall.setLayoutManager(manager);
        adapter = new MusicHallHorAdapter(getContext());
        recyclerView_hall.setAdapter(adapter);
        loadData();
        loadBanner();

    }
    private void loadData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<RecommendResource> call = service.getRecommendResource(LoadingActivity.cookie);
        call.enqueue(new Callback<RecommendResource>() {
            @Override
            public void onResponse(Call<RecommendResource> call, Response<RecommendResource> response) {
                RecommendResource baseResponse = response.body();
                recommendBeanList = baseResponse.getRecommend();
                adapter.setData(recommendBeanList);
            }

            @Override
            public void onFailure(Call<RecommendResource> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    private void loadBanner() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<com.example.myapplication.entity.Banner> call = service.getBanner(1);
        call.enqueue(new Callback<com.example.myapplication.entity.Banner>() {
            @Override
            public void onResponse(Call<com.example.myapplication.entity.Banner> call, Response<com.example.myapplication.entity.Banner> response) {
                com.example.myapplication.entity.Banner baseResponse = response.body();
                bannerList = baseResponse.getBanners();
                initBanner();
            }

            @Override
            public void onFailure(Call<com.example.myapplication.entity.Banner> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

}
